package com.lgl.scs.mobile.controller;

import com.github.pagehelper.PageInfo;
import com.lgl.scs.base.controller.BaseController;
import com.lgl.scs.mobile.service.MServiceOrderService;
import com.lgl.scsapi.system.model.AppUser;
import com.lgl.scsapi.system.model.PageData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 订单controller
 */
@RestController
@RequestMapping(value="/mServiceOrder")
public class MServiceOrderController extends BaseController{
	private Logger log = LoggerFactory.getLogger(MServiceOrderController.class);
	@Resource(name="mServiceOrderService")
	private MServiceOrderService mServiceOrderService;

	//请求移动端主页的服务订单数据,暂时不考虑从redis中取数据
	@RequestMapping(value="/myAllOrder",produces="application/json;charset=UTF-8")
	public Object myAllOrder() {
		PageData allPd = this.getPageData();
		int pageSize = Integer.parseInt(allPd.remove("showCount").toString()),
			pageNo = Integer.parseInt(allPd.remove("currentPage").toString());
		//取得城市编码
		AppUser appUser = this.getSessionAppUser();
		if (appUser == null){
			allPd.clear();
			allPd.put("status",-1);
			return allPd;
		}
		String userId = allPd.getString("userId"), status = allPd.getString("status"),
				getUserId = allPd.getString("getUserId"),statusArray = allPd.getString("statusArray");
		//如果是获取发布/未抢/已抢订单，则不用getOrRob查询条件
		if (StringUtils.hasText(userId) || StringUtils.hasText(getUserId)){
			if (!StringUtils.hasText(status) && !StringUtils.hasText(statusArray)){
				allPd.put("noEqualsStatus","3");
			}
		}else {
			allPd.put("getOrRob","T");
			allPd.put("issueOrRobUser",appUser.getUserId());
		}
		PageInfo orderPage = mServiceOrderService.pageServiceOrder(pageNo,pageSize,allPd);
		allPd.clear();
		allPd.put("status",0);
		allPd.put("orderPage", orderPage);
		return allPd;
	}
	//编辑服务订单信息
	@RequestMapping(value="/robberDoneOrder",produces="application/json;charset=UTF-8")
	public String robberDoneOrder() {
		PageData robberDonePd = this.getPageData();
		try {
			AppUser appUser = this.getSessionAppUser();
			if (appUser == null){
				robberDonePd.clear();
				return "-1";
			}
			return mServiceOrderService.robberDoneOrder(appUser.getUserId(),robberDonePd);
		} catch (Exception e) {
			robberDonePd.clear();
			log.error("移动：抢单人完成订单操作失败：",e);
		}
		return "6";
	}
	//发布人完成服务订单操作
	@RequestMapping(value="/issuerDoneOrder",produces="application/json;charset=UTF-8")
	public String issuerDoneOrder() {
		PageData pd = this.getPageData();
		String issuerDoneStatus;
		try {
			AppUser appUser = this.getSessionAppUser();
			issuerDoneStatus = mServiceOrderService.issuerDoneOrder(appUser.getUserId(),pd);
		} catch (Exception e) {
			pd.clear();
			issuerDoneStatus = "6";
			log.error(e.toString(),e);
		}
		return issuerDoneStatus;
	}
	//删除服务订单
	@RequestMapping(value="/deleteOrder",produces="application/json;charset=UTF-8")
	public String deleteOrder(){
		PageData deletePd = this.getPageData();
		String deleteState;
		try {
			deleteState = mServiceOrderService.deleteServiceOrder(getSessionAppUser(),deletePd);
		} catch (Exception e) {
			deleteState = "6";
			log.error(e.toString(),e);
		}
		deletePd.clear();
		return deleteState;
	}
	//取消服务订单信息
	@RequestMapping(value="/cancelServiceOrder",produces="application/json;charset=UTF-8")
	public String cancelServiceOrder() {
		PageData pd = this.getPageData();
		String cancelState;
		try {
			AppUser cancelUser = getSessionAppUser();
			cancelState = mServiceOrderService.cancelServiceOrder(cancelUser,pd);
		} catch (Exception e) {
			cancelState = "6";
			log.error(e.toString(),e);
		}
		pd.clear();
		return cancelState;
	}

	//提交用户评价信息
	@RequestMapping(value="/submitUserRate",produces="application/json;charset=UTF-8")
	public Object submitUserRate() throws Exception {
		PageData pd = this.getPageData();
		String levelNum = pd.getString("levelNum"),userImpression = pd.getString("userImpression"),
				billId = pd.getString("billId"),rateText = pd.getString("rateText");//, rateIssueOrRob = pd.getString("rateIssueOrRob");
		if (!StringUtils.hasText(levelNum) || !StringUtils.hasText(userImpression) ||
				!StringUtils.hasText(billId) || !StringUtils.hasText(rateText)) {//|| !StringUtils.hasText(rateIssueOrRob)
			return "1";
		}
		String result = mServiceOrderService.saveUserRate(pd,getSessionAppUser());
		pd.clear();
		return result;
	}
}